package com.foreveross.crawl.corn;

import java.util.Date;
import java.util.UUID;

import org.quartz.DateBuilder;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.calendar.DailyCalendar;
import org.quartz.impl.triggers.SimpleTriggerImpl;

public class SimpleCronTriggerBuilder {
	public static final String DEFAULT_WORKER_TRIGGER_NAME="DEFAULT_WORKER_TRIGGER_NAME_";
	public static final String DEFAULT_WORKER_TRIGGER_GROUP_NAME="DEFAULT_WORKER_TRIGGER_GROUP_NAME";
	/**
	 * 创建一个简单的带时间极值的SimpleTrigger
	 * @param interval
	 * @param start
	 * @param end
	 * @return
	 */
	public  static Trigger buildSimpleTriggerAddDailyCalendar(Scheduler scheduler,long interval,String start,String end)throws Exception{
		SimpleTriggerImpl trigger=new SimpleTriggerImpl(DEFAULT_WORKER_TRIGGER_NAME+UUID.randomUUID().toString()
				, DEFAULT_WORKER_TRIGGER_GROUP_NAME, new Date(),null, Integer.MAX_VALUE, interval);
		DailyCalendar dailyCalendar=new DailyCalendar(start, end);
		dailyCalendar.setInvertTimeRange(true); //这个必须设为True  
		String calName=UUID.randomUUID().toString()+System.currentTimeMillis();
		scheduler.addCalendar(calName, dailyCalendar, false, false);
		trigger.setStartTime(DateBuilder.nextGivenSecondDate(new Date(), 30));
		trigger.setCalendarName(calName);
		return trigger;
	}
}
